package cn.com.homeyoda.aimate.aihealth.views;

import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import cn.com.homeyoda.aimate.aihealth.R;

public class SlideRecyclerView2 extends RecyclerView {

    private static final int INVALID_POSITION = -1;
    private int mSlidePosition = INVALID_POSITION;
    private float mDownX;
    private float mDownY;
    private View mSlideView;
    private int mMaxSlideWidth;

    public SlideRecyclerView2(Context context) {
        this(context, null);
    }

    public SlideRecyclerView2(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SlideRecyclerView2(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        mMaxSlideWidth = dp2px(80); // 删除按钮宽度
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        int x = (int) e.getX();
        int y = (int) e.getY();

        switch (e.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mDownX = e.getX();
                mDownY = e.getY();
                // 查找点击的item位置
                View itemView = findChildViewUnder(x, y);
                if (itemView != null) {
                    ViewHolder viewHolder = getChildViewHolder(itemView);
                    mSlidePosition = viewHolder.getAdapterPosition();
                    mSlideView = itemView.findViewById(R.id.content);
                }
                break;
        }
        return super.onInterceptTouchEvent(e);
    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        if (mSlidePosition == INVALID_POSITION || mSlideView == null) {
            return super.onTouchEvent(e);
        }

        float x = e.getX();
        float dx = x - mDownX;

        switch (e.getAction()) {
            case MotionEvent.ACTION_MOVE:
                // 只处理水平滑动
                if (Math.abs(dx) > dp2px(5)) {
                    // 限制滑动范围
                    if (dx < 0 && dx > -mMaxSlideWidth) {
                        mSlideView.setTranslationX(dx);
                    }
                    return true;
                }
                break;

            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                // 滑动超过一半则显示完整删除按钮，否则恢复
                if (dx < -mMaxSlideWidth / 2) {
                    mSlideView.animate().translationX(-mMaxSlideWidth);
                } else {
                    mSlideView.animate().translationX(0);
                }
                mSlidePosition = INVALID_POSITION;
                break;
        }
        return super.onTouchEvent(e);
    }

    private int dp2px(float dp) {
        return (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                dp,
                getResources().getDisplayMetrics()
        );
    }
}
